const AipOcrClient = require("baidu-aip-sdk");
const { baiduConfig } = require("./config");

// Initialize the client once
const client = new AipOcrClient.ocr(
  baiduConfig.APP_ID,
  baiduConfig.API_KEY,
  baiduConfig.SECRET_KEY
);

exports.BaiduOCRService = {
  /**
   * 识别身份证
   * @param image 图片base64字符串
   * @param idCardSide 身份证正反面 "front"/"back"
   */
  async recognizeIDCard(image, idCardSide) {
    try {
      const options = {};
      const result = await client.idcard(image, idCardSide, options);
      return result;
    } catch (error) {
      throw new Error(`身份证识别失败: ${error.message}`);
    }
  },

  /**
   * 识别营业执照
   * @param image 图片base64字符串
   */
  async recognizeBusinessLicense(image) {
    try {
      const options = {};
      const result = await client.businessLicense(image, options);
      return result;
    } catch (error) {
      throw new Error(`营业执照识别失败: ${error.message}`);
    }
  }
}; 